package com.quasar.hpatient.module.home_bloodpressure;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.dialog.CalendarDialog;
import com.quasar.hpatient.dialog.InputDoubleDialog;
import com.quasar.hpatient.dialog.TimeDialog;
import com.quasar.hpatient.dialog.WarningDialog;
import com.quasar.hpatient.module.home_daily.HomeDailyActivity;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.context.BaseConstant;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.chart.ChartView;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 首页 ==> 血压
 * created by kalu on 2018/3/29 9:03
 */
public class BloodpressureActivity extends BaseActivity<BloodpressurePresenter> implements BloodpressureView {

    public static final String MULIT = "MULIT";
    public static final String DATE = "DATE";

    @Override
    public void onBackPressed() {

        final int intExtra = getIntent().getIntExtra(HomeDailyActivity.POSITION, -1);
        final Intent intent = getPresenter().getResult(this, intExtra);
        setResult(HomeDailyActivity.RESULT_CODE, intent);
        super.onBackPressed();
    }

    @Override
    public int initView() {
        return R.layout.activity_home_bloodpressure;
    }

    @Override
    public void initDataLocal() {

        final boolean mulit = getIntent().getBooleanExtra(MULIT, false);

        final NavigationView menu = findViewById(R.id.home_bloodpressure_menu);
        menu.setMenuEnable(!mulit);
        if (mulit) {
            menu.setOnBackChangeListener(() -> onBackPressed());
        } else {
            menu.setOnNavigationChangeListener((isBack, isMenu) -> {
                if (isBack) onBackPressed();
                else if (isMenu) {
                    final RecyclerView recycler = findViewById(R.id.activity_home_bloodpressure_list);
                    final ChartView chart = findViewById(R.id.activity_home_bloodpressure_chart);
                    final EditText edit1 = findViewById(R.id.activity_home_bloodpressure_input1);
                    final EditText edit2 = findViewById(R.id.activity_home_bloodpressure_input2);
                    getPresenter().saveBloodpressure(this, chart, recycler, edit1, edit2);
                }
            });
        }

        final RecyclerView recycler = findViewById(R.id.activity_home_bloodpressure_list);
        getPresenter().initList(this, recycler, mulit);

        findViewById(R.id.activity_home_bloodpressure_date).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_bloodpressure_menu3).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_bloodpressure_menu4).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_bloodpressure_menu5).setVisibility(mulit ? View.GONE : View.VISIBLE);

        if (!mulit) {
            findViewById(R.id.activity_home_bloodpressure_input1).requestFocus();
            findViewById(R.id.activity_home_bloodpressure_time).setOnClickListener(v -> showTime());
            TextView date = findViewById(R.id.activity_home_bloodpressure_date);
            TextView time = findViewById(R.id.activity_home_bloodpressure_time);
            final String stringExtra = getIntent().getStringExtra(DATE);
            if (TextUtils.isEmpty(stringExtra)) {
                getPresenter().initTime(this, date, time);
            } else {
                setText(date, stringExtra);
                getPresenter().initTime(this, null, time);
            }
            date.setOnClickListener(v -> {

                final String trim = date.getText().toString().trim();
                try {
                    final int day = Integer.parseInt(trim.substring(trim.length() - 2, trim.length()));
                    showCalendar(date, day);
                } catch (Exception e) {
                    LogUtil.e("", e.getMessage(), e);
                }
            });
        } else {
            TextView date = findViewById(R.id.activity_home_bloodpressure_date);
            final String stringExtra = getIntent().getStringExtra(DATE);
            setText(date, stringExtra);
        }
    }

    @Override
    public void initDataNet() {
        final RecyclerView recycler = findViewById(R.id.activity_home_bloodpressure_list);
        final ChartView chart = findViewById(R.id.activity_home_bloodpressure_chart);
        final boolean mulit = getIntent().getBooleanExtra(MULIT, false);
        getPresenter().bloodpressureList(this, chart, recycler, mulit);
    }

    @Override
    public void clearInput() {

        EditText input1 = findViewById(R.id.activity_home_bloodpressure_input1);
        if (null != input1) {
            input1.setText(BaseConstant.NULL_STR);
        }
        EditText input2 = findViewById(R.id.activity_home_bloodpressure_input2);
        if (null != input2) {
            input2.setText(BaseConstant.NULL_STR);
        }
    }

    @Override
    public void showInput(final int position, String high, String low) {

        final InputDoubleDialog dialog = new InputDoubleDialog(this);
        dialog.show();
        dialog.setMenu(R.string.home_bloodpressure_hyperpiesia, R.string.home_bloodpressure_lowtension);
        dialog.setTitle("修改血压");
        dialog.setInput(high, low);
        dialog.setOnDialogChangeListener((top, bottom) -> {
            final RecyclerView recycler = findViewById(R.id.activity_home_bloodpressure_list);
            final ChartView chart = findViewById(R.id.activity_home_bloodpressure_chart);
            getPresenter().modifyBloodpressure(BloodpressureActivity.this, chart, recycler, position, top, bottom);
        });
    }

    @Override
    public void showDelete(int position) {

        final WarningDialog dialog = new WarningDialog(this);
        dialog.show();
        dialog.setTitles("确定删除血压");
        dialog.setButton("取消", "确定");
        dialog.setOnDialogChangeListener((left, right) -> {
            if (!right) return;
            final RecyclerView recycler = findViewById(R.id.activity_home_bloodpressure_list);
            final ChartView chart = findViewById(R.id.activity_home_bloodpressure_chart);
            getPresenter().deleteBloodpressure(BloodpressureActivity.this, chart, recycler, position);
        });
    }

    @Override
    public void showTime() {

        final TimeDialog dialog = new TimeDialog(this);
        dialog.show();
        dialog.setTitles("选择时间");
        dialog.setOnTimeChangeListener((hour, minute) -> {
            final TextView text = findViewById(R.id.activity_home_bloodpressure_time);
            text.setText(hour + ":" + minute);
        });
    }

    @Override
    public void showCalendar(TextView text, int day) {

        final CalendarDialog dialog = new CalendarDialog(this);
        dialog.setScroll(getDate());
        dialog.show();
        dialog.setOnCalendarChangeListener((years, months, days, maxDay, isClick) -> {
            if (isClick) {
                final TextView date = findViewById(R.id.activity_home_bloodpressure_date);
                setText(date, years + "-" + months + "-" + days);
                final RecyclerView recycler = findViewById(R.id.activity_home_bloodpressure_list);
                final ChartView chart = findViewById(R.id.activity_home_bloodpressure_chart);
                final boolean mulit = getIntent().getBooleanExtra(MULIT, false);
                getPresenter().bloodpressureList(this, chart, recycler, mulit);
            } else {
                getPresenter().schemelList(dialog, years, months, days, maxDay);
            }
        });
    }

    @Override
    public String getDate() {
        final TextView text = findViewById(R.id.activity_home_bloodpressure_date);
        return text.getText().toString();
    }

    @Override
    public String getTime() {
        final TextView text = findViewById(R.id.activity_home_bloodpressure_time);
        return text.getText().toString();
    }

    @Override
    public String getHigh() {
        final TextView text = findViewById(R.id.activity_home_bloodpressure_input1);
        return text.getText().toString();
    }

    @Override
    public String getLow() {
        final TextView text = findViewById(R.id.activity_home_bloodpressure_input2);
        return text.getText().toString();
    }
}